package jmine.tec.persist.naturalKey;

import java.io.Serializable;

import jmine.tec.persist.naturalkey.InvalidNaturalKeyException;
import jmine.tec.persist.naturalkey.NaturalKeyHandler;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import bancosys.tec.persist.Persistable;
import bancosys.tec.persist.entities.domain.Instituicao;

/**
 * {@link NaturalKeyHandler} implementation targeting the test class {@link Instituicao}. 
 * 
 * @author seiti
 * Jun 8, 2012
 */
public class InstituicaoNaturalKeyHandler implements NaturalKeyHandler {

    /**
     * {@inheritDoc}
     */
    public void addRestrictionEq(Criteria criteria, Serializable naturalKey) throws InvalidNaturalKeyException {
        String mnemonico = naturalKey == null ? null : naturalKey.toString();
        Criteria criteriaAgente = criteria.createCriteria("agente");
        criteriaAgente.add(Restrictions.eq("mnemonico", mnemonico));
    }

    /**
     * {@inheritDoc}
     */
    public void addRestrictionLike(Criteria criteria, Serializable naturalKey, MatchMode matchMode) throws InvalidNaturalKeyException {
        String mnemonico = naturalKey == null ? null : naturalKey.toString();
        Criteria criteriaAgente = criteria.createCriteria("agente");
        criteriaAgente.add(Restrictions.ilike("mnemonico", mnemonico, MatchMode.ANYWHERE));
        criteriaAgente.addOrder(Order.asc("mnemonico"));
    }

    /**
     * {@inheritDoc}
     */
    public Serializable getNaturalKey(Persistable persistable) {
        return ((Instituicao) persistable).getAgente().getMnemonico();
    }

    /**
     * {@inheritDoc}
     */
    public void addOrderAsc(Criteria criteria) {
        //Ok
    }

    /**
     * {@inheritDoc}
     */
    public Class<? extends Persistable> getTargetClass() {
        return Instituicao.class;
    }

}
